<?php
namespace models\repositories;
use Doctrine\ORM\EntityRepository;
use entities;
class SectorRepository extends EntityRepository {

	/**
     * @inheritdoc
     */
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        $orderBy = $orderBy === null ? array('id' => 'desc') : $orderBy;
        return parent::findBy($criteria, $orderBy, $limit, $offset);
    }
	
	public function findAll($orderBy=null){		
		return $this->findBy(array(), $orderBy);
	}
	
	public function findAllAsObject(){
		$qb = $this->_em->createQueryBuilder();		
		$qb->select('Kategori')
		   ->from('models\entities\Kategori', 'Kategori')
		;
		
		$query = $qb->getQuery();
		return $query->getResult();
	}

	public function findChildOfCategory($id){
		$dql = 'SELECT e
				FROM models\entities\Category e
				LEFT JOIN e.categoryPath p 
				WHERE e.parent >=:parent
				ORDER BY p.path DESC';
		$query = $this->_em->createQuery($dql);
		$query->setParameter('parent', $id);
		return $query->getResult(); 
	}
}

